#!/bin/bash
#-2太快
#-6次数用完
#-7未登录
if ! grep -q "一加社区抽奖：" ~/app-root/repo/log/sign.log; then
    echo "一加社区抽奖：未抽奖" >> ~/app-root/repo/log/sign.log
elif grep -q "一加社区抽奖：$(date -d "8 hour" -u "+%Y年%m月%d日")" ~/app-root/repo/log/sign.log; then
    exit
fi
cd ~/app-root/data/temp/sign/
getJYsum=0
useragent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0"
for ((i=1;i<=10;i++)); do
curl "http://www.oneplusbbs.com/plugin.php?id=choujiang&do=draw" --user-agent "$useragent" -H "Accept: application/json, text/javascript, */*; q=0.01" --Cookie "$(grep '^Cookie' www.oneplusbbs.com|awk -F ': ' '{print $2}')" > result.txt
if grep -q 'ret\":-7' result.txt; then
    curl "$(grep '^loginUrl' $i|awk -F ': ' '{print $2}')" --user-agent "$useragent" --data "$(grep '^loginData' $i|awk -F ': ' '{print $2}')" -D log.txt 2>/dev/null
    sed -i "/Cookie:\ /s#ONEPLUSID.*#$(grep -o 'ONEPLUSID=[a-zA-Z0-9_%]*' log.txt)#" $i
    curl "http://www.oneplusbbs.com/plugin.php?id=choujiang&do=draw" --user-agent "$useragent" -H "Accept: application/json, text/javascript, */*; q=0.01" --Cookie "$(grep '^Cookie' www.oneplusbbs.com|awk -F ': ' '{print $2}')" > result.txt
fi
return="$(python ~/app-root/runtime/data/github-sh/tool/unicode.py "$(grep -o '\\u[a-zA-Z0-9\\]*' result.txt|head -1)")！"
if grep -q 'ret\":-6' result.txt; then
    break
elif grep -q 'ret\":-2' result.txt; then
    i=$(($i-1))
elif echo "$return" | grep -q "遗憾" ; then
    continue
elif echo "$return" | grep -q "加油" ; then
    getJY=$(grep -o 'get_jiayou\":[0-9]*' result.txt|grep -o '[0-9]*')
    getJYsum=$(($getJY+$getJYsum))
else
    msg="$msg$return"
fi
if [ "$i" -lt "10" ]; then sleep 5; fi
done
if [ "$getJYsum" -gt "0" ]; then
sed -i "/一加社区抽奖：/s#：.*#：$(date -d "8 hour" -u "+%Y年%m月%d日 %H:%M") 共抽奖$i次，获得$getJYsum个加油！$msg#" ~/app-root/repo/log/sign.log
fi
#rm -f result.txt